cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
set(RADIX_LIBRARY RadixEngine)

if (COVERAGE)
  message("-- Coverage Enabled")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  --coverage -fprofile-arcs -ftest-coverage")
endif ()

if(NOT DEFINED CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build type")
endif()

project(${RADIX_LIBRARY} CXX C)
set(CMAKE_CXX_STANDARD 14)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

#### UTILITY ####

set(RADIX_DEFINITIONS "")
function(radix_add_exported_definition)
  add_definitions("-D${ARGV0}")
  list(APPEND RADIX_DEFINITIONS "${ARGV0}")
  set(RADIX_DEFINITIONS "${RADIX_DEFINITIONS}" PARENT_SCOPE)
endfunction(radix_add_exported_definition)

#### COMPILE-TIME SETTINGS ####

set(RADIX_GL_LOADER "glad" CACHE STRING "OpenGL loader to use. One of 'libepoxy', 'glad'")

if ("${RADIX_GL_LOADER}" STREQUAL "libepoxy")
  set(RADIX_GL_USE_LIBEPOXY TRUE)
  radix_add_exported_definition("RADIX_GL_USE_LIBEPOXY")
endif()
if ("${RADIX_GL_LOADER}" STREQUAL "glad")
  set(RADIX_GL_USE_GLAD TRUE)
  radix_add_exported_definition("RADIX_GL_USE_GLAD")
endif()

#### PLATFORM-SPECIFIC FIXES ####

if(DEFINED WIN32)
  add_definitions("-D_USE_MATH_DEFINES")
endif()

#### LIBRARIES ####

set(RADIX_INCLUDE_DIRS "")

find_package(OpenGL REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR})
list(APPEND RADIX_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/external/stx/include)

set(RADIX_LIBRARIES ${OPENGL_gl_LIBRARY})

find_package(PkgConfig REQUIRED)

if (RADIX_GL_USE_LIBEPOXY)
  pkg_search_module(EPOXY REQUIRED epoxy)
  list(APPEND RADIX_INCLUDE_DIRS ${EPOXY_INCLUDE_DIRS})
  list(APPEND RADIX_LIBRARIES ${EPOXY_LIBRARIES})
elseif (RADIX_GL_USE_GLAD)
  set(GLAD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/glad")
  set(GLAD_SRC "${GLAD_INCLUDE_DIR}/glad.c")
  add_library("GLAD" ${GLAD_SRC})
  list(APPEND RADIX_INCLUDE_DIRS ${GLAD_INCLUDE_DIR})
  list(APPEND RADIX_LIBRARIES GLAD)
endif()

find_package(Assimp REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${ASSIMP_INCLUDE_DIRS})
list(APPEND RADIX_LIBRARIES ${ASSIMP_LIBRARIES} )

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${SDL2_INCLUDE_DIR} ${SDL2_MIXER_INCLUDE_DIRS})
list(APPEND RADIX_LIBRARIES ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARIES} )

find_package(TinyXML2 2.0.0 REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${TINYXML2_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${TINYXML2_LIBRARIES})
add_definitions("-DTINYXML2_MAJOR_VERSION=${TINYXML2_MAJOR_VERSION}")
add_definitions("-DTINYXML2_MINOR_VERSION=${TINYXML2_MINOR_VERSION}")
add_definitions("-DTINYXML2_PATCH_VERSION=${TINYXML2_PATCH_VERSION}")
add_definitions("-DTINYXML2_VERSION_CODE=${TINYXML2_VERSION_CODE}")

find_package(json11 REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${JSON11_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${JSON11_LIBRARY})

find_package(Catch REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${CATCH_INCLUDE_DIR})

find_package(Linenoise REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${LINENOISE_INCLUDE_DIR})

find_package(Bullet 2.82 REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${BULLET_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${BULLET_LIBRARIES})

find_package(VHACD REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${VHACD_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${VHACD_LIBRARY})

find_package(FreeImage REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${FREEIMAGE_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${FREEIMAGE_LIBRARIES})

find_package(AngelScript REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${ANGELSCRIPT_INCLUDE_DIRS})
list(APPEND RADIX_LIBRARIES ${ANGELSCRIPT_LIBRARY})

find_package(easy_profiler REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${EASY_PROFILER_INCLUDE_DIR})
set(RADIX_ENABLE_PROFILER ON CACHE BOOL "Enable Radix's profiler")
if (RADIX_ENABLE_PROFILER)
  list(APPEND RADIX_LIBRARIES ${EASY_PROFILER_LIBRARIES})
else ()
  radix_add_exported_definition("DISABLE_EASY_PROFILER")
endif ()

set(PORTAL_TEST_LIBRARIES ${RADIX_LIBRARIES})

list(APPEND RADIX_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/external/serine/include")

#### ENGINE ####

if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  add_definitions("-Wall")
endif ()

find_package(RadixEntity REQUIRED)
list(APPEND RADIX_INCLUDE_DIRS ${RADIXENTITY_INCLUDE_DIR})
list(APPEND RADIX_LIBRARIES ${RADIXENTITY_LIBRARIES})

set(RADIX_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
list(APPEND RADIX_INCLUDE_DIRS "${RADIX_INCLUDE_DIR}")

include_directories(${RADIX_INCLUDE_DIRS})

add_subdirectory(source)

add_library(${RADIX_LIBRARY} STATIC ${RADIX_SOURCES})
target_link_libraries(${RADIX_LIBRARY} ${RADIX_LIBRARIES} dl)

# Export
list(APPEND RADIX_LIBRARIES ${RADIX_LIBRARY})

if (RADIX_EXPORT_CMAKE_PACKAGE_VARS)
  set(RADIX_LIBRARY "${RADIX_LIBRARY}" PARENT_SCOPE)
  set(RADIX_LIBRARIES "${RADIX_LIBRARIES}" PARENT_SCOPE)
  set(RADIX_INCLUDE_DIR "${RADIX_INCLUDE_DIR}" PARENT_SCOPE)
  set(RADIX_INCLUDE_DIRS "${RADIX_INCLUDE_DIRS}" PARENT_SCOPE)
  set(RADIX_DEFINITIONS "${RADIX_DEFINITIONS}" PARENT_SCOPE)
endif ()

#### UNIT TESTING ####

include_directories(${RADIX_INCLUDE_DIRS} ${UnitTestPlusPlus_INCLUDE_DIRS})
enable_testing()
add_subdirectory(tests)

if (CMAKE_VERSION VERSION_GREATER 2.8.5)
  include(FeatureSummary)
  feature_summary(WHAT ALL)
endif ()
